package com.xinya.wanandroidclient.ui.square

import android.util.Log
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import com.xinya.wanandroidclient.Constants
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.adapter.SquareNavAdapter
import com.xinya.wanandroidclient.adapter.SquareSystemAdapter
import com.xinya.wanandroidclient.base.LazyBaseFragment
import com.xinya.wanandroidclient.databinding.FragmentNavlistBinding
import com.xinya.wanandroidclient.http.LoadState

/**
 *
 * @ClassName:      SquareListFragment
 * @Description:     java类作用描述
 * @Author:         mhzhuxinya@163.com
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
class SquareListFragment : LazyBaseFragment() {
    override fun getLayoutId() = R.layout.fragment_navlist

    companion object {
        private const val TAG = "SquareListFragment"
    }

    private var fragmentType = 0

    private val squareNavAdapter by lazy {
        SquareNavAdapter()
    }

    private val squareSystemAdapter by lazy {
        SquareSystemAdapter()
    }

    private val viewModel by lazy {
        getFragmentVieModel(SquareViewModel::class.java)
    }

    private lateinit var dataBinding: FragmentNavlistBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentNavlistBinding
    }

    override fun observer() {
        val navSmartRefreshView = dataBinding.navSmartRefresh

        viewModel.apply {
            navListViewModel.observe(this@SquareListFragment) {
                Log.d(TAG, "observer: " + it.size)
                squareNavAdapter.setNewInstance(it)
            }

            systemListViewModel.observe(this@SquareListFragment) {
                Log.d(TAG, "observer: " + it.size)
                squareSystemAdapter.setNewInstance(it)
            }

            refreshStateLiveData.observe(this@SquareListFragment) {
                when (it) {
                    LoadState.Error -> {
                        // smartRefresh数据加载失败
                        smartRefreshState(navSmartRefreshView, false)
                        Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show()
                    }
                    LoadState.Success -> {
                        // smartRefresh数据加载成功
                        smartRefreshState(navSmartRefreshView, true)
                    }
                    else -> {
                    }
                }
            }
        }
    }

    override fun onClick() {
        squareSystemAdapter.setFlexChildClickListener { textView, id, string ->
            Log.d(TAG, "onClick: " + textView.text + " --> " + string)
            Toast.makeText(context, textView.text, Toast.LENGTH_SHORT).show()
        }

        squareNavAdapter.setFlexChildClickListener { textView, id, string ->
            Log.d(TAG, "onClick: " + textView.text + " --> " + string)
            Toast.makeText(context, textView.text, Toast.LENGTH_SHORT).show()
        }

    }

    override fun lazyInitView() {
        fragmentType = arguments?.getInt("type", 0) ?: 0

        if (fragmentType == 0) {
            return
        }

        val navSmartRefreshView = dataBinding.navSmartRefresh
        val recyclerListView = dataBinding.recyclerList
        recyclerListView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = if (fragmentType == Constants.NAV_PROJECT) {
                squareSystemAdapter
            } else {
                squareNavAdapter
            }
        }

        navSmartRefreshView.apply {
            // 自动刷新
            autoRefresh()
            // 使能加载更多
            setEnableLoadMore(false)
            // 使能重新加载
            setEnableRefresh(true)
            // 滚动回弹
            setEnableOverScrollDrag(true)
            // 重新加载监听
            setOnRefreshListener {
                viewModel.getClassList(fragmentType)
            }

        }
    }
}